﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Sunny.Entity;
using Sunny.Model.RequestParam;
using Sunny.Util.Exception;

namespace Sunny.Service
{
    public class DictTypeService
    {
        private readonly SunnyContext _sunnyContext;
        private readonly UserService _userService;

        public DictTypeService(SunnyContext sunnyContext, UserService userService)
        {
            _sunnyContext = sunnyContext;
            _userService = userService;
        }



        public async Task<(List<SysDictType>, int)> GetList(DictTypeListParam param)
        {
            var dictTypes = _sunnyContext.SysDictTypes.AsQueryable();
            if (!string.IsNullOrEmpty(param.DictName))
            {
                dictTypes = dictTypes.Where(t => t.DictName.Contains(param.DictName));
            }

            if (!string.IsNullOrEmpty(param.DictType))
            {
                dictTypes = dictTypes.Where(t => t.DictType.Contains(param.DictType));
            }

            if (!string.IsNullOrEmpty(param.Status))
            {
                dictTypes = dictTypes.Where(t => t.Status == param.Status);
            }

            if (param.CreateTime != null && param.CreateTime.Length == 2)
            {
                dictTypes = dictTypes.Where(t =>
                    t.CreateTime >= param.CreateTime[0] && t.CreateTime <= param.CreateTime[1]);
            }

            var total = await dictTypes.CountAsync();
            var dictTypeList = await dictTypes.OrderBy(t => t.DictId).Skip((param.PageNum - 1) * param.PageSize)
                .Take(param.PageSize).ToListAsync();
            return (dictTypeList, total);
        }

        public async Task<SysDictType> GetById(long dictTypeId)
        {
            return await _sunnyContext.SysDictTypes.SingleOrDefaultAsync(t => t.DictId == dictTypeId);
        }

        public async Task Update(DictTypeEditParam param)
        {
            var sysDictType = await _sunnyContext.SysDictTypes.SingleOrDefaultAsync(t => t.DictId == param.DictId);
            sysDictType.DictName = param.DictName;
            sysDictType.DictType = param.DictType;
            sysDictType.Status = param.Status;
            sysDictType.Remark = param.Remark;

            await _sunnyContext.SaveChangesAsync();
        }

        public async Task Add(SysDictType sysDictType)
        {
            var currentUser = _userService.GetCurrentUser();
            await _sunnyContext.SysDictTypes.AddAsync(sysDictType);
            await _sunnyContext.SaveChangesAsync();
        }

        public async Task Delete(string typeIds)
        {
            string[] ids = typeIds.Split(',');
            foreach (var id in ids)
            {
                SysDictType sysDictType = await _sunnyContext.SysDictTypes.SingleOrDefaultAsync(t => t.DictId == long.Parse(id));
                var count = await _sunnyContext.SysDictData.CountAsync(t => t.DictType == sysDictType.DictType);
                if (count > 0)
                {
                    throw new BusinessException(CodeMessage.CanNotDelete.SetMessage($"{sysDictType.DictName}已分配，不能删除"));
                }
                _sunnyContext.Remove(sysDictType);
            }

            await _sunnyContext.SaveChangesAsync();
        }
    }
}